package com.sghd.candy.module.fight.manager;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.ramcache.enhance.Enhance;
import com.sghd.common.utils.math.MathUtils;
import com.sghd.pojo.candy.fight.enums.BattleResult;
import com.sghd.pojo.fight.game.RoomType;

import javax.persistence.*;
import java.util.Date;

/**
 * @author Allen
 * @date 2018/12/03
 */
@Entity
@Table(catalog = "bsdld")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_10_MINUTE))
@NamedNativeQueries({
        @NamedNativeQuery(name = FightInfo.RANK_BY_GRADE,
                query = "SELECT p.playerId, pvpWinRate, pvpGrade, nickName, headIconId FROM bsdld.fightinfo f JOIN bsdld.player p where f.playerId = p.playerId and pvpCount >= 100 ORDER BY pvpGrade DESC LIMIT 0,20"),
        @NamedNativeQuery(name = FightInfo.RANK_BY_WINRATE,
                query = "SELECT p.playerId, pvpWinRate,pvpGrade, nickName, headIconId FROM bsdld.fightinfo f JOIN bsdld.player p where f.playerId = p.playerId and pvpCount >= 100 ORDER BY pvpWinRate DESC LIMIT 0,20")
})
public class FightInfo implements IEntity<Long> {
    public static final String RANK_BY_GRADE = "rank_by_grade";
    public static final String RANK_BY_WINRATE = "rank_by_winRate";
    /**
     * 玩家Id
     */
    @Id
    private Long playerId;
    /**
     * 当前战斗服Id
     */
    @Column(length = 2)
    private Integer serverId;
    /**
     * 最近一次进入时间
     */
    private Date lastTime;
    /**
     * pvp场次
     */
    private Integer pvpCount;
    /**
     * pvp胜利次数
     */
    private Integer pvpWin;
    /**
     * pvp积分
     */
    private Integer pvpGrade;
    /**
     * pve积分
     */
    private Integer pveGrade;
    @Enumerated(EnumType.ORDINAL)
    private RoomType roomType;
    /**
     * PVP胜率
     */
    private Float pvpWinRate;

    public Long getPlayerId() {
        return playerId;
    }

    void setPlayerId(Long playerId) {
        this.playerId = playerId;
    }

    public Integer getServerId() {
        return serverId;
    }

    void setServerId(Integer serverId) {
        this.serverId = serverId;
    }

    public Date getLastTime() {
        return lastTime;
    }

    void setLastTime(Date lastTime) {
        this.lastTime = lastTime;
    }

    public Integer getPvpCount() {
        return pvpCount;
    }

    void setPvpCount(Integer pvpCount) {
        this.pvpCount = pvpCount;
    }

    public Integer getPvpWin() {
        return pvpWin;
    }

    void setPvpWin(Integer pvpWin) {
        this.pvpWin = pvpWin;
    }

    public Integer getPvpGrade() {
        return pvpGrade;
    }

    void setPvpGrade(Integer pvpGrade) {
        this.pvpGrade = pvpGrade;
    }

    public Integer getPveGrade() {
        return pveGrade;
    }

    public void setPveGrade(Integer pveGrade) {
        this.pveGrade = pveGrade;
    }

    public RoomType getRoomType() {
        return roomType;
    }

    void setRoomType(RoomType roomType) {
        this.roomType = roomType;
    }

    public Float getPvpWinRate() {
        return pvpWinRate;
    }

    void setPvpWinRate(Float winRate) {
        this.pvpWinRate = winRate;
    }

    public static FightInfo valueOf(long playerId) {
        FightInfo result = new FightInfo();
        result.playerId = playerId;
        result.serverId = -1;
        result.pvpCount = 0;
        result.pvpWin = 0;
        result.pvpGrade = 0;
        result.pveGrade = 0;
        result.pvpWinRate = 0.0f;
        return result;
    }

    public static FightInfo valueOf(long playerId, int serverId) {
        FightInfo result = valueOf(playerId);
        result.serverId = serverId;
        return result;
    }

    /**
     * 连接战斗服
     *
     * @param serverId
     */
    @Enhance
    void connect(int serverId, RoomType roomType) {
        this.serverId = serverId;
        this.roomType = roomType;
        lastTime = new Date();
    }

    /**
     * 与战斗服断开连接
     */
    @Enhance
    void disConnect() {
        serverId = -1;
//        roomType = RoomType.NONE;
    }

    @Enhance
    void pvpSettlement(BattleResult battleResult) {
        this.pvpCount = this.pvpCount + 1;
        if (battleResult == BattleResult.WIN) {
            this.pvpWin = this.pvpWin + 1;
            this.pvpGrade = this.pvpGrade + 3;
        } else {
            this.pvpGrade = this.pvpGrade + 1;
        }
        if (pvpCount > 0 ) {
            this.pvpWinRate = (float) MathUtils.round(pvpWin * 1.0 / pvpCount,3);
        } else {
            this.pvpWinRate = 0F;
        }
    }

    @Enhance
    void pveSettlement(int addGrade) {
        this.pveGrade = this.pveGrade + addGrade;
    }

    @Override
    public Long getId() {
        return this.playerId;
    }
}
